package com.fc.controller;

import com.fc.common.util.FileUtil;
import com.fc.common.util.RandomImageUtil;
import com.fc.common.util.RandomUtil;
import com.fc.config.AutoGenerateCodeUtil;
import com.fc.dto.CodeGenarateDTO;
import com.fc.vo.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;


/**
 * 公用控制器
 */
@Controller
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private AutoGenerateCodeUtil autoGenerateCodeUtil;

    @Value("${upload-files}")
    private  String uploadPath;

    @ApiOperation("获取验证码")
    @GetMapping(value = "/randomImage")
    @ResponseBody
    public Result<String> randomImage(){
        Result<String> res = new Result<String>();
        try {
            String code = RandomUtil.getCode();
            res.setMessage(code);
            res.setSuccess(true);
            res.setResult(RandomImageUtil.generate(code));
        } catch (IOException e) {
            res.error500("获取验证码出错");
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public Result<?> upload(@RequestParam(name = "file") MultipartFile file, HttpServletRequest request, @RequestParam(name = "catalogue") String catalogue){
        String orgName = null;
        String base = request.getScheme() + "://" +
                request.getServerName() + ":" +
                request.getServerPort() +
                request.getContextPath() + "/";
        try {
            orgName = file.getOriginalFilename();
            String suffix = orgName.substring(orgName.indexOf('.'));
            while(new File(uploadPath+catalogue+"/"+orgName).exists()){
                orgName = orgName.replace(suffix,"_1"+suffix);
            }
            String newFileName = uploadPath+catalogue+"/"+orgName;
            File newFile = new File(newFileName);
            //将文件写入项目
            OutputStream os = null;
            InputStream is = null;

            try {
                os = new FileOutputStream(newFile);
                is =file.getInputStream();
                FileUtil.copyFile(is,os);
            }catch (Exception e){
                e.printStackTrace();
                throw  e;
            }finally {
                FileUtil.close(is,os);
            }
            return Result.OK(base+"upload/"+catalogue+"/"+orgName);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.OK("");
        }
    }

    @GetMapping("/test")
    @ResponseBody
    public String test1(){
        CodeGenarateDTO dto  = new CodeGenarateDTO();
        dto.setAuthor("zffc");
        dto.setDbType("mysql");
        dto.setInclude("sys_page_set");
        dto.setParent("com.fc");
        dto.setModuleName(null);
        autoGenerateCodeUtil.autoGenerateCode(dto);
        return "{\"status\":\"" + "success" + "\"}";
    }

}
